//
//  NetworkMessage.m
//  Networking
//
//  Created by Christoph Eßer on 11/29/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NetworkMessage.h"


@implementation NetworkMessage

@synthesize type,messageID,data;

-(id)initWithType: (MessageType) aType msgId: (u_int32_t) msgId data:(NSObject <NSCoding>*) aData {
    self = [super init];
    if(self) {
        type = aType;
        messageID = msgId;
        data = [aData retain];
    }
    
    return self;
}

-(id)initWithType: (MessageType) aType data:(NSObject <NSCoding>*) aData {
    self = [super init];
    if(self) {
        type = aType;
        messageID = arc4random();
        data = [aData retain];
    }
    
    return self;
}

-(id) initWithString:(NSString*)str{
    self = [super init];
    if(self){
        type = kStringMessage;
        messageID = arc4random();
        data = [str retain];
    }
    return self;
}

-(id) initWithStatus:(NSString *)status{
    self = [super init];
    if(self){
        type = kStatus;
        messageID = arc4random();
        data = [status retain];
    }
    return self;
}

-(id)initWithReconnect:(NSString*)connID{
    self = [super init];
    if(self){
        type = kReconnectMessage;
        messageID = arc4random();
        data = [connID retain];
    }
    return self;
}

-(id)initWithConnected:(NSString*)connID{
    self = [super init];
    if(self){
        type = kConnectedMessage;
        messageID = arc4random();
        data = [connID retain];
    }
    return self;
}

-(id)initWithDisconnect{
    self = [super init];
    if(self){
        type = kDisconnectMessage;
        messageID = arc4random();
        data = nil;
    }
    return self;
}

-(id)initWithQuery{
    self = [super init];
    if(self){
        type = kQueryMessage;
        messageID = arc4random();
        data = nil;
    }
    return self;
}

-(id)initWithUserlist:(NSMutableDictionary *)users {
    self = [super init];
    if(self){
        type = kUserlistMessage;
        messageID = arc4random();
        data = [users retain];
    }
    
    return self;
}

-(id)initWithTarget:(NSObject<NSCoding>*) aData withTarget:(NSString *)clientID {
    self = [super init];
    if(self){
        type = kMessageWithTarget;
        messageID = arc4random();
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:aData, @"data", clientID, @"target",  nil];
        data = [dict retain];
    }
    
    return self;
}

-(id)initWithSender:(NSObject<NSCoding> *)aData withSender:(NSString *)clientID{
    self = [super init];
    if(self){
        type = kMessageWithSender;
        messageID = arc4random();
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:aData, @"data", clientID, @"sender",  nil];
        data = [dict retain];
    }
    
    return self;
}



- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        //NSLog(@"Adecoder %@",aDecoder);
        type = [aDecoder decodeIntForKey:@"type"];
        messageID = [aDecoder decodeInt32ForKey:@"msgId"];
        data = [[aDecoder decodeObjectForKey:@"data"] retain];
    }
    
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeInt:type forKey:@"type"];
    [aCoder encodeInt32:messageID forKey:@"msgId"];
    [aCoder encodeObject:data forKey:@"data"];
}

-(void) dealloc {
    [data release];
}

-(NSString*)stringMessage {
    switch (type) {
        case kStringMessage:
            return [data description];
            break;
            
        case kConnectedMessage:
            return [data description];
            break;
            
        case kReconnectMessage:
            return [data description];
            break;
            
        default:
            return [data description];
    }
}

@end
